package zy.blue7.common.vaildate.custom;


import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.SmartValidator;
import zy.blue7.common.vo.req.SaveUserDto;

/**
 * @author blue7
 * @version 1.0
 * @date 2021/11/22 10:15
 * todo 自定义验证器，制定验证规则
 * <p>
 * https://blog.csdn.net/f641385712/article/details/97270786  todo 讲的不错
 * https://blog.csdn.net/f641385712/category_10338056.html?spm=1001.2014.3001.5482
 */
@Component
public class MySmartValidator<T extends SaveUserDto> implements SmartValidator {

    private T target;

    /**
     * todo 支持分组的验证
     *
     * @param target
     * @param errors
     * @param validationHints
     */
    @Override
    public void validate(Object target, Errors errors, Object... validationHints) {

        doValidate((T) target, errors, validationHints);
    }


    @Override
    public boolean supports(Class<?> clazz) {
        return true;
    }

    /**
     * todo 使用默认分组的验证器
     *
     * @param target
     * @param errors
     */
    @Override
    public void validate(Object target, Errors errors) {
        doValidate((T) target, errors, null);
    }

    private void doValidate(T target, Errors errors, Object[] validationHints) {
        /**
         * 进行验证，可以从数据库查询，等等各种条件判断
         */

        if (1 == 0) {
            errors.reject("44444", "错误了");
        }
    }
}
